
#include "loginform.h"
#include "def.h"


LoginForm::LoginForm()
{
	setWindowFlags( windowFlags() & ~Qt::WindowMaximizeButtonHint);
	//setFixedSize(350 ,160);
	SIZE_BASE = 70;

	// create interface
	createInterface();

	// connect signals
	connect(m_btnLogin, SIGNAL(clicked()), this, SLOT(slotLogin()));
	connect(m_btnCancel, SIGNAL(clicked()), this, SLOT(slotCancel()));
}

LoginForm::~LoginForm()
{

}

void LoginForm::createInterface()
{
	// labels
	m_lblUser = new QLabel(tr(LABEL_USER));
	m_lblPassword = new QLabel(tr(LABEL_PASSWORD));
	//m_lblURLBase = new QLabel(tr(LABEL_URL));

	// line edit
	m_txtUser = new QLineEdit();
	m_txtUser->setFixedWidth(SIZE_BASE*3);
	m_txtPassword = new QLineEdit();
	m_txtPassword->setEchoMode(QLineEdit::Password);
	m_txtPassword->setFixedWidth(SIZE_BASE*3);
	m_txtURLBase= new QLineEdit();
	m_txtURLBase->setFixedWidth(SIZE_BASE*3);
	m_txtURLBase->setText(tr(DEFAULT_URL));

	// buttons
	m_btnLogin =  new QPushButton(tr(BUTTON_LOGIN));
	m_btnCancel = new QPushButton(tr(BUTTON_CANCEL));

	// group
	m_grpLogin = new QGroupBox();

	// add push button to ground layout
	QHBoxLayout *p_btnlayout = new QHBoxLayout();
	p_btnlayout->addWidget(m_btnLogin);
	p_btnlayout->addWidget(m_btnCancel);

	QGridLayout *p_grLayout = new QGridLayout();
	p_grLayout->setColumnMinimumWidth(0, SIZE_BASE);
	p_grLayout->setColumnMinimumWidth(1, SIZE_BASE*3);
#if !defined(_WIN32) && !defined(_WIN64)
	p_grLayout->setContentsMargins(1, 1, 1, 1);
#endif
	//set layout for ground
	m_grpLogin->setLayout(p_grLayout);

	int row = 0;
	//add label widget to ground layout
	//p_grLayout->addWidget(m_lblURLBase, row, 0);
	//p_grLayout->addWidget(m_txtURLBase, row++, 1, Qt::AlignLeft );
	p_grLayout->addWidget(m_lblUser, row, 0);
	p_grLayout->addWidget(m_txtUser, row++, 1, Qt::AlignLeft );
	p_grLayout->addWidget(m_lblPassword, row ,0);
	p_grLayout->addWidget(m_txtPassword, row++, 1, Qt::AlignLeft );

	p_grLayout->addLayout(p_btnlayout, row++, 1, Qt::AlignRight);

	//main widget
	m_layMainLayout = new QHBoxLayout();
	m_layMainLayout->addWidget(m_grpLogin);

	// login form
	setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
	setLayout(m_layMainLayout);
	setWindowTitle(tr(TITLE_FORM_LOGIN));
	setStyleSheet("QLabel {font-weight: bold;}");

	// message box title
	m_msgBox.setWindowTitle(tr(TITLE_FORM_MSGBOX));

#ifdef DEBUG
	m_txtUser->setText("wcrdj");
	m_txtPassword->setText("wcrdj1");
#endif

}

void LoginForm::showMessage(QString text)
{
	m_msgBox.setStyleSheet(styleSheet() + m_msgBox.styleSheet());
	m_msgBox.setIcon(QMessageBox::Information);
	m_msgBox.setText(text);
	m_msgBox.setStandardButtons(QMessageBox::Ok);
	m_msgBox.exec();
}

int LoginForm::showQuestion(QString text)
{
	m_msgBox.setStyleSheet(styleSheet() + m_msgBox.styleSheet());
	m_msgBox.setIcon(QMessageBox::Question);
	m_msgBox.setText(text);
	m_msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
	return m_msgBox.exec();
}

void LoginForm::enableInputs(bool bEnabled)
{
	m_txtURLBase->setEnabled(bEnabled);
	m_txtUser->setEnabled(bEnabled);
	m_txtPassword->setEnabled(bEnabled);
	m_btnLogin->setEnabled(bEnabled);
	m_btnCancel->setEnabled(bEnabled);
	updatesEnabled();
}

// slots
void LoginForm::slotLogin()
{
	enableInputs(false);

	QString url = m_txtURLBase->text(), user = m_txtUser->text(), pwd = m_txtPassword->text();
	emit onLogin(url.toLatin1(), user.toLatin1(), pwd.toLatin1());

	enableInputs(true);
}

void LoginForm::slotCancel()
{
	m_bIsCancel = true;
	emit onCancel();
}

int LoginForm::exec()
{
	m_bIsCancel = false;
	return QDialog::exec();
}

bool LoginForm::isCancel()
{
	return m_bIsCancel;
}

